import re
import argparse
from pathlib import Path

VERSION_FILE = Path("zsl_ma/version.py")


def bump_version(version_type="patch"):
    # 读取当前版本（指定UTF-8编码）
    content = VERSION_FILE.read_text(encoding="utf-8")
    match = re.search(r'__version__ = "(\d+\.\d+\.\d+)"', content)
    if not match:
        raise ValueError("Invalid version format")

    major, minor, patch = map(int, match.group(1).split('.'))

    # 递增版本号
    if version_type == "major":
        major += 1
        minor = 0
        patch = 0
    elif version_type == "minor":
        minor += 1
        patch = 0
    else:  # patch
        patch += 1

    new_version = f"{major}.{minor}.{patch}"

    # 更新版本文件（指定UTF-8编码）
    new_content = re.sub(
        r'__version__ = "\d+\.\d+\.\d+"',
        f'__version__ = "{new_version}"',
        content
    )
    VERSION_FILE.write_text(new_content, encoding="utf-8")
    return new_version


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Bump package version")
    parser.add_argument("--type",
                        choices=["major", "minor", "patch"],
                        default="patch",
                        help="Version increment type")
    args = parser.parse_args()

    new_ver = bump_version(args.type)
    print(f"Version bumped to {new_ver}")
